package com.example.gokchinesefoodmapmcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gokchinesefoodmapmcdev.dao.GoodsInfoDAO;
import com.example.gokchinesefoodmapmcdev.dao.GoodsPicDAO;
import com.example.gokchinesefoodmapmcdev.dao.GoodsSkuDAO;
import com.example.gokchinesefoodmapmcdev.dao.MerchantInfoDAO;
import com.example.gokchinesefoodmapmcdev.entity.goods.GoodsInfo;
import com.example.gokchinesefoodmapmcdev.entity.goods.GoodsPic;
import com.example.gokchinesefoodmapmcdev.entity.goods.GoodsSku;
import com.example.gokchinesefoodmapmcdev.entity.MerchantInfo;
import com.example.gokchinesefoodmapmcdev.service.MerchantService;
import com.example.gokchinesefoodmapmcdev.util.MyTranslateTools;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantInfoDAO merchantInfoDAO;

    @Autowired
    private GoodsInfoDAO goodsInfoDAO;

    @Autowired
    private GoodsSkuDAO goodsSkuDAO;

    @Autowired
    private GoodsPicDAO goodsPicDAO;

    /**
     * 查询商户信息
     * @param merchantId  商户id
     * @return
     */
    @Override
    public MerchantInfo findMerchantInfoById(Integer merchantId) {
        MerchantInfo merchantInfo = merchantInfoDAO.selectOne(new QueryWrapper<MerchantInfo>().eq("id", merchantId));
        return merchantInfo;
    }

    /**
     * 根据商户id查询该商户的商品信息，需分页
     *      需要的商品信息来源：
     *          1. 商品信息表 2. 判断部分中文值 3. 商品库存表 4. 商品图片表
     * @param merchantId    商户id
     * @param pageNumber    当前页码数
     * @param pageSize      一页显示数据量
     * @return
     */
    @Override
    public List<Map<String, Object>> findGoodsListByMerchantId(Integer merchantId, Integer pageNumber, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(pageNumber, pageSize);

        // 1. 商品信息
        List<GoodsInfo> goodsInfos = goodsInfoDAO.selectList(new QueryWrapper<GoodsInfo>().eq("merchant_id", merchantId));
        List<Map<String, Object>> result = new ArrayList<>();
        // 2. 遍历得到的商品，填充其他字段值
        for (GoodsInfo goodsInfo : goodsInfos) {
            Map<String, Object> map = MyTranslateTools.transEntityToMap(goodsInfo);
            MyTranslateTools.addStatusZn(map, goodsInfo);

            // 3. 查询图片表，取出第一张图片
            List<GoodsPic> goodsPics
                    = goodsPicDAO.selectList(new QueryWrapper<GoodsPic>().eq("good_id", goodsInfo.getId()).orderByAsc("id"));
            if(!goodsPics.isEmpty()) {
                map.put("picture", goodsPics.get(0).getPicPath());
            }else {
                map.put("picture", null);
            }

            // 4. 查询库存信息，取出最低价、库存、销量等信息
            List<GoodsSku> goodsSkus
                    = goodsSkuDAO.selectList(new QueryWrapper<GoodsSku>().eq("good_id", goodsInfo.getId()).orderByAsc("price"));
            int stock = 0, sales = 0;
            if(goodsSkus.isEmpty()){
                map.put("minPrice", null);
            }else {
                map.put("minPrice", goodsSkus.get(0).getPrice());
                for (GoodsSku goodsSku : goodsSkus) {
                    stock += goodsSku.getStock();
                    sales += goodsSku.getSales();
                }
            }
            map.put("stock", stock);
            map.put("sales", sales);
            // 最低价的规格信息
            map.put("specifications", goodsSkus.get(0).getAttrSymbolText());

            result.add(map);
        }
        return result;
    }

}
